package com.example.estest.config;


import java.util.concurrent.*;

public  class RetryPolicy implements RejectedExecutionHandler {

    private DelayQueue<PullJobDelayTask> queue = new DelayQueue<>();

    private int i = 0;

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        if(r instanceof MyThread){
            MyThread thread = (MyThread) r;
        }
        queue.offer(new PullJobDelayTask(5, TimeUnit.SECONDS, r));
        if(i>0){
            return;
        }
        CompletableFuture.runAsync(()-> {
            while (true) {
                try {
                    PullJobDelayTask task = queue.take();
                    executor.execute(task.runnable);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        i++;
    }
}



